package com.uvnos4j.matilda.system.controller;

import com.uvnos4j.matilda.commons.annotation.AppLog;
import com.uvnos4j.matilda.commons.dto.QueryParam;
import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.commons.utils.TreeUtil;
import com.uvnos4j.matilda.system.domain.SysAcl;
import com.uvnos4j.matilda.system.domain.SysRole;
import com.uvnos4j.matilda.system.domain.SysUser;
import com.uvnos4j.matilda.system.service.SysAclService;
import com.uvnos4j.matilda.system.service.SysRoleService;
import com.uvnos4j.matilda.system.service.SysUserService;
import com.uvnos4j.matilda.system.vo.AclVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限 Controller
 * <p>
 * Description:权限 Controller
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @since 2020-08-23 21:45:01
 */
@Controller
@RequestMapping("/sys/acl")
public class SysAclController {

    @Autowired
    private SysAclService sysAclService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * @return index
     */
    @AppLog(title = "权限管理")
    @PreAuthorize("hasAuthority('sys:acl')")
    @GetMapping("/list")
    public String index() {
        return "sys/acllist";
    }

    @PreAuthorize("hasAuthority('sys:acl')")
    @ResponseBody
    @GetMapping
    public Result aclTree() {
        // 得到所有的查询参数，注意也包括分页信息和排序信息
        QueryParam qp = RequestUtil.getQueryParam();
        List<SysAcl> aclList = sysAclService.list(qp);
        List<SysAcl> treeList = TreeUtil.bulidTree(aclList);
        return Result.success(treeList);
    }

    @AppLog(title = "添加权限")
    @PreAuthorize("hasAuthority('sys:acl:add')")
    @ResponseBody
    @PostMapping()
    public Result add(@Valid AclVo aclVo) {
        SysAcl sysAcl = aclVo.buildModel(SysAcl.class, false);
        boolean b = sysAclService.save(sysAcl);
        if (b) {
            return Result.success("权限添加成功", sysAcl);
        }
        return Result.fail("权限添加失败");
    }

    @AppLog(title = "修改权限")
    @PreAuthorize("hasAuthority('sys:acl:update')")
    @ResponseBody
    @PutMapping()
    public Result update(@Valid AclVo aclVo) {
        SysAcl sysAcl = aclVo.buildModel(SysAcl.class, true);
        boolean b = sysAclService.updateById(sysAcl);
        if (b) {
            return Result.success("权限修改成功", aclVo.getId());
        }
        return Result.fail("权限修改失败");
    }

    @AppLog(title = "删除权限")
    @PreAuthorize("hasAuthority('sys:acl:del')")
    @ResponseBody
    @DeleteMapping()
    public Result deletes(@RequestParam("ids") List<String> ids) {
        boolean b = sysAclService.removeByIds(ids);
        if (b) {
            return Result.success("权限删除成功,删除[" + ids.size() + "]条记录");
        }
        return Result.fail("权限删除失败");
    }

    @PreAuthorize("hasAuthority('sys:acl')")
    @ResponseBody
    @RequestMapping(value = "/findUserAndRole", method = RequestMethod.GET)
    public Result findUserAndRole(@RequestParam Long aclId) {
        List<SysUser> userList = sysUserService.findByAclId(aclId);
        List<SysRole> roleList = sysRoleService.findByAclId(aclId);
        Map<String, Object> data = new HashMap<>(16);
        data.put("userList", userList);
        data.put("roleList", roleList);
        return Result.success(data);
    }


}
